#include "fyp/ResourcePool.h"

//------------------------------------------------------------------------------
// additional header includes
#include "fyp/render/Factory.h"

//using namespace ming::fyp::render;

//------------------------------------------------------------------------------
namespace ming
{
namespace fyp
{
	//--------------------------------------------------------------------------
	TResourcePool::TResourcePool(render::TResourceFactory *resFactory) :
		mResFactory(resFactory)
	{
	}
	
	//--------------------------------------------------------------------------
	TResourcePool::~TResourcePool()
	{
		RemoveAllResources();
	}

	//--------------------------------------------------------------------------
	template<>
	TResourcePool::Pool_t<
		TResourcePool::Traits_t<render::IRenderable>
	>& 
	TResourcePool::Pool<render::IRenderable>()
	{
		return mRenderablePool;
	}

	//----------------------------------
	template<>
	TResourcePool::Pool_t<
		TResourcePool::Traits_t<render::IShader>
	>& 
	TResourcePool::Pool<render::IShader>()
	{
		return mShaderPool;
	}

	//----------------------------------
	template<>
	TResourcePool::Pool_t<
		TResourcePool::Traits_t<render::ITexture>
	>& 
	TResourcePool::Pool<render::ITexture>()
	{
		return mTexturePool;
	}

	//----------------------------------
	template<>
	TResourcePool::Pool_t<
		TResourcePool::Traits_t<render::IRenderedTexture>
	>& 
	TResourcePool::Pool<render::IRenderedTexture>()
	{
		return mRenderedTexPool;
	}

	//--------------------------------------------------------------------------
	void TResourcePool::RemoveAllResources()
	{
		RemoveByType<render::IRenderable>();
		RemoveByType<render::IShader>();
		RemoveByType<render::ITexture>();
		RemoveByType<render::IRenderedTexture>();
	}

	//--------------------------------------------------------------------------
	template<>
	void TResourcePool::Add<render::IRenderable>(
		const TStr &alias,
		typename Traits_t<render::IRenderable>::Param const &param)
	{
		Resource_t< Traits_t<render::IRenderable> > res;

		res.res = CreateXMesh(
			param.c_str(),
			mResFactory);

		res.param = param;

		Pool<render::IRenderable>()[alias] = res;
	}

	//----------------------------------
	template<>
	void TResourcePool::Add<render::IShader>(
		const TStr &alias,
		typename Traits_t<render::IShader>::Param const &param)
	{
		Resource_t< Traits_t<render::IShader> > res;

		res.res = CreateShader(
			get<0>(param).c_str(),
			get<1>(param).c_str(),
			mResFactory);

		res.param = param;

		Pool<render::IShader>()[alias] = res;
	}

	//----------------------------------
	template<>
	void TResourcePool::Add<render::ITexture>(
		const TStr &alias,
		typename Traits_t<render::ITexture>::Param const &param)
	{
		Resource_t< Traits_t<render::ITexture> > res;

		res.res = CreateTexture(
			param.c_str(),
			mResFactory);

		res.param = param;

		Pool<render::ITexture>()[alias] = res;
	}

	//----------------------------------
	template<>
	void TResourcePool::Add<render::IRenderedTexture>(
		const TStr &alias,
		typename Traits_t<render::IRenderedTexture>::Param const &param)
	{
		Resource_t< Traits_t<render::IRenderedTexture> > res;

		res.res = CreateRenderedTexture(
			get<0>(param),
			get<1>(param),
			get<2>(param),
			get<3>(param),
			mResFactory);

		res.param = param;

		Pool<render::IRenderedTexture>()[alias] = res;
	}

	//--------------------------------------------------------------------------
}
}
